package com.yangxiangrice.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yangxiangrice.pojo.Result;
import com.yangxiangrice.pojo.UserList;
import com.yangxiangrice.service.MenuService;
import com.yangxiangrice.service.UserListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: TODO
 * @author: shixiaojian
 * @date: 2021年11月24日 16:04
 */
@Controller
@RequestMapping("/menu")
@CrossOrigin
@Slf4j
public class MenuController {
    @Autowired
    MenuService menuService;
    @Autowired
    UserListService userListService;

    @GetMapping(value = "/list")
    @ResponseBody
    public Result list() {
        Result<List> result = new Result<>();
        Subject subject = SecurityUtils.getSubject();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", subject.getPrincipal());
        UserList user = userListService.getOne(wrapper);
        wrapper.eq("userType", user.getUserType());
        List list = menuService.list(wrapper);
        if (!ObjectUtil.isEmpty(list)) {
            result.ok(list);
        } else {
            result.fail("加载菜单失败");
        }
        return result;
    }
}
